<template>
  <div class="ticket-item">
    <div class="check" v-if="options.mode == 'default'">
      <el-checkbox :value="check" @change="changeCheck"></el-checkbox>
    </div>
    <div class="info" @click="$router.push({'path':'/ticket-view?ticketId=' + ticket.ticketId})">
      <div class="title" v-text="ticket.title"></div>
      <div class="detail">
        <div class="customer" v-text="ticket.customerName"></div>
        <div class="created" v-text="getRelativeTime(ticket.gmtCreate)"></div>
        <div class="deadline" v-if="ticket.deadline" v-text="ticket.deadline"></div>
      </div>
    </div>
    <div class="option" v-if="options.mode == 'default'">
      <div class="view item"  @click="$router.push({'path':'/ticket-view?ticketId=' + ticket.ticketId})">
        <i class="el-icon-view"></i>
      </div>
      <div class="edit item" @click="$router.push({'path':'/ticket-edit?ticketId=' + ticket.ticketId})">
        <i class="el-icon-edit"></i>
      </div>
      <div class="delete item">
        <i class="el-icon-delete"></i>
      </div>
    </div>
    <div class="status mobile" :class="ticket.status == 'open' ? 'open' : 'closed'" v-text="ticket.status == 'open' ? 'Open' : 'Closed'">
    </div>
    <div v-if="options.mode == 'default'" class="status pc" :class="ticket.status == 'open' ? 'open' : 'closed'">
      <el-select v-model="ticket.status" @change="changeTicketStatus">
        <el-option value="open" label="Open"></el-option>
        <el-option value="closed" label="Closed"></el-option>
      </el-select>
    </div>
    <div class="comments" @click="$router.push({'path':'/ticket-view?ticketId=' + ticket.ticketId})">
      <div class="icon">
        <i class="el-icon-chat-line-round"></i>
      </div>
      <div class="count" v-if="ticket.commentCount > 0" v-text="ticket.commentCount"></div>
    </div>
    <el-tooltip v-if="options.mode == 'default'" class="item" effect="dark" :content="ticket.assigneeName" placement="bottom">
      <div class="assignee" v-text="ticket.assigneeName ? ticket.assigneeName.substr(0,1) : ''">
      </div>
    </el-tooltip>
  </div>
</template>

<script>
import {getRelativeTime} from "@/libs/tools";
import {changeTicketStatus} from "@/api/ticket";

export default {
  name: "ticket-item",
  props: {
    ticket: {
      type: Object,
      default() {
        return {}
      }
    },
    options: {
      type:Object,
      default() {
        return {mode:'default'}
      }
    },
    checkWatch: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      check: false
    }
  },
  created() {
    this.ticket.oldStatus = this.ticket.status
  },
  methods: {
    getRelativeTime(time) {
      return getRelativeTime(time)
    },
    changeCheck(val) {
      this.check = val
      if(val) {
        this.$emit('check',this.ticket)
      }else {
        this.$emit('uncheck',this.ticket)
      }
    },
    changeTicketStatus(val) {
      changeTicketStatus(this.ticket.ticketId,this.ticket.oldStatus,val).then(res => {
        if(res.code == 200) {
          this.ticket.oldStatus = val
          this.$message.success('更改状态成功')
        }else{
          this.ticket.status =  this.ticket.oldStatus
        }
      })
    }
  },
  watch: {
    checkWatch : function (newVal, oldVal) {
      this.check = this.checkWatch
    }
  }
}
</script>

<style scoped lang="less">
.ticket-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #F0F0F0;
  position: relative;
  overflow: hidden;
  &:hover {
    background: #F8F8F8;
    .option {
      opacity: 1;
    }
  }
  .check {
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }
  .option {
    flex-shrink: 0;
    display: flex;
    opacity: 0;
    transition: opacity .2s;
    .item {
      margin-right: 8px;
      cursor: pointer;
    }
    .edit:hover {
      color: #0091ff;
    }
    .delete:hover {
      color: red;
    }

    @media(max-width: 679px) {
      padding: 0 5px;
    }
  }
  @media(max-width: 679px) {
    .option {
      opacity: 1;
    }
  }
  .info {
    flex: 1;
    overflow: hidden;
    cursor: pointer;
    .title {
      overflow: hidden;
      text-overflow: ellipsis;
      word-break: keep-all;
    }
    .detail {
      display: flex;
      font-size: 12px;
      color: #666666;
      flex-wrap: wrap;
      .customer {
        color: #0091ff;
        cursor: pointer;
        margin-right: 10px;
      }
    }
  }
  .status {
    font-size: 12px;
    width: 50px;
    text-align: center;
    font-weight: bold;
    margin: 0 10px;
    &.open{
      color: #0091ff;
      /deep/.el-select {
        .el-input {
          input {
            color: #0091ff !important;
          }
        }
      }
    }
    &.closed {
      color: #FF3824;
      /deep/.el-select {
        .el-input {
          input {
            color: #FF3824 !important;
          }
        }
      }
    }
    &.mobile {
      display: none;
    }
    &.pc {
      /deep/.el-select {
        width: 100%;
        .el-input {
          display: flex;
          input {
            color: #333333;
            border: none;
            transition: none;
            padding: 0;
            width: 48px;
            text-align: center;
            user-select:none;
            background: transparent !important;
            &::selection{
              background:transparent;
            }

            // firefox
            &::-moz-selection{
              background:transparent;
            }
          }
          .el-input__suffix {
            position: relative;
            .el-select__caret {
              color: #333333;
            }
          }
        }
      }
    }
    @media(max-width: 679px) {
      position: absolute;
      right: -45px;
      top: 6px;
      height: 20px;
      line-height: 20px;
      transform: rotate(45deg);
      text-align: center;
      width: 100px;
      text-align: center;
      &.open{
        background: #0091ff;
        color: #FFFFFF;
      }
      &.closed {
        background: #FF3824;
        color: #FFFFFF;
      }
      &.default {
        border: 1px solid #F0F0F0;
      }
      &.pc {
        display: none;
      }
      &.mobile {
        display: block;
      }
    }
  }
  .comments {
    padding: 0 10px;
    margin: 0 10px;
    position: relative;
    cursor: pointer;
    .count {
      position: absolute;
      top: -8px;
      right: -2px;
      background: #0091ff;
      width: 20px;
      height: 20px;
      line-height: 20px;
      text-align: center;
      font-size: 12px;
      color: #FFFFFF;
      border-radius: 50%;
      transform: scale(.65);
    }
    @media(max-width: 679px) {
      padding: 0;
      .count {
        right: -10px;
      }
    }
  }
  .assignee {
    flex-shrink: 0;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    border-radius: 50%;
    cursor: pointer;
    border: 1px solid #F0F0F0;
    margin: 0 10px;

    @media(max-width: 679px) {
      height: 35px;
      width: 35px;
      line-height: 35px;
      font-size: 14px;
    }
  }
}

/deep/ .el-select-dropdown {
  min-width: 100px !important;
}
</style>
